// 状态码可选项
const stateOptions = {
	Success: 200,
	Fail: 400,
	Authentication: 401,
	NotFound: 404,
};

// 函数参数类型
interface params {
	data?: any;
	code?: keyof typeof stateOptions;
	msg?: string;
}

// 函数类型
export type formatFunType = ({ code, data, msg }: params) => {
	data: any;
	code: number;
	msg: string;
};

const formatResJsonFun: formatFunType = ({ data = null, code = 'Success', msg = '' } = {}) => {
	if (!stateOptions[code]) throw new Error('code参数错误');
	return { data, code: stateOptions[code], msg };
};

global.formatResJson = formatResJsonFun;
